package com.example.smartagriculture.service.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.smartagriculture.entity.City;
import com.example.smartagriculture.mapper.CityMapper;
import com.example.smartagriculture.service.ICityService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.web.util.UriUtils;

import javax.annotation.Resource;
import javax.naming.Name;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * CityServiceImpl 服务实现类
 * @author 张芸博
 * @since 2025-03-07
 */
@Service
public class CityServiceImpl extends ServiceImpl<CityMapper, City> implements ICityService {
    @Resource
    CityMapper cityMapper;
    public static String URL = "https://api.map.baidu.com/weather/v1/?";

    public static String AK = "fRFfhKo2ItsJ8oAanQlDPLlazujuTirq";

    @Override
    public List<City> select(String Name) {
        QueryWrapper<City> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().like(City::getName ,Name);
        return cityMapper.selectList(queryWrapper);
    }

    @Override
    public String Weather(String Name) throws Exception{
        QueryWrapper<City> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(City::getName,Name);
        City city = this.getOne(queryWrapper);
        String districtId = city.getId().toString();

        CityServiceImpl snCal = new CityServiceImpl();

        Map<String, String> params = new LinkedHashMap<>();
        params.put("district_id", districtId);
        params.put("data_type", "all");
        params.put("ak", AK);
        return snCal.requestGetAK(URL, params);
    }
    public String requestGetAK(String strUrl, Map<String, String> param) throws IOException {
        if (strUrl == null || strUrl.length() <= 0 || param == null || param.size() <= 0) {
            return null;
        }

        StringBuffer queryString = new StringBuffer();
        queryString.append(strUrl);
        for (Map.Entry<?, ?> pair : param.entrySet()) {
            queryString.append(pair.getKey() + "=");
            //    第一种方式使用的 jdk 自带的转码方式  第二种方式使用的 spring 的转码方法 两种均可
            //    queryString.append(URLEncoder.encode((String) pair.getValue(), "UTF-8").replace("+", "%20") + "&");
            queryString.append(UriUtils.encode((String) pair.getValue(), "UTF-8") + "&");
        }

        if (queryString.length() > 0) {
            queryString.deleteCharAt(queryString.length() - 1);
        }

        java.net.URL url = new URL(queryString.toString());
        System.out.println(queryString.toString());
        URLConnection httpConnection = (HttpURLConnection) url.openConnection();
        httpConnection.connect();

        InputStreamReader isr = new InputStreamReader(httpConnection.getInputStream());
        BufferedReader reader = new BufferedReader(isr);
        StringBuffer buffer = new StringBuffer();
        String line;
        while ((line = reader.readLine()) != null) {
            buffer.append(line);
        }
        reader.close();
        isr.close();
        System.out.println("AK: " + buffer.toString());
        return buffer.toString();
    }
}
